import { createApp } from 'vue'
import Vuex from "vuex"
import App from "@/App.vue";

const Vue = createApp(App)

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        role: [],//角色
        detailData: [],
        orderId: 0,
        changedOrderId: 0,
        changedKnowledgeId: 0,
        user: '',
        loginUserNickname: '',
        loginUserAvatar: '',
        activeIndex: '1',
        publishActiveIndex: '1',
        userActiveIndex: '1-1',
        token: localStorage.getItem('token') ? localStorage.getItem('token') : '',
        paymentInfo: '',
        imgShowRoad: 'http://localhost:9090',
        fileUploadRoad: 'http://localhost:9090'
    },
    getters: {
        getStorage(state) {
            //获取token
            if (!state.token) {
                state.token = JSON.parse(localStorage.getItem(key))
                return state.token
            }
        },
        isExpert(state) {
            //是否是专家
            for (const k in state.role) {
                if (state.role[k].authority === "expert") {
                    return true;
                }
            }
            return false;
        },
        isAdmin(state) {
            //是否是管理员
            for (const k in state.role) {
                if (state.role[k].authority === "admin") {
                    return true;
                }
            }
            return false;
        },
    },
    mutations: {
        updateOrderId(state, value) {
            state.orderId = value
        },
        updateDetailData(state, value) {
            state.detailData = value
        },
        updateChangedOrderId(state, value) {
            state.changedOrderId = value
        },
        updateChangedKnowledgeId(state, value) {
            state.changedKnowledgeId = value
        },
        updateLoginUserNickname(state, value) {
            state.loginUserNickname = value
        },
        updateLoginUserAvatar(state, value) {
            state.loginUserAvatar = value
        },
        updateRole(state, value) {
            state.role = value
        },
        updatePaymentInfo(state, value) {
            state.paymentInfo = value
        },
        updateActiveIndex(state, value) {
            state.activeIndex = value
        },
        updatePublishActiveIndex(state, value) {
            state.publishActiveIndex = value
        },
        updateUserActiveIndex(state, value) {
            state.userActiveIndex = value
        },
        // 设置存储token
        setToken(state, value) {
            state.token = value;
            localStorage.setItem('token', value)
        },
        // 删除token
        removeStorage(state) {
            localStorage.removeItem('token');
        }
    },
    actions: {
    },
    modules: {
    }
})